package com.lei.service.impl;/**
 * @Auther: zhaoss
 * @Date: 2022/5/9 - 05 - 09 - 9:28
 * @Description: com.lei.service.impl
 * @version: 1.0
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lei.constant.RedisConstant;
import com.lei.dao.SetmealDao;
import com.lei.entity.PageResult;
import com.lei.pojo.Setmeal;
import com.lei.service.SetmealService;
import com.lei.utils.QinUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@ClassName SetmealServiceImpl
 *@Description TODO
 *@Author Admin
 *@Date 2022/5/9 9:28
 *@Version 1.0
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

    //新增套餐，同时需要关联检查组
    public void add (Setmeal setmeal,Integer[] checkgroupIds){
        setmealDao.add(setmeal);
        if(checkgroupIds !=null && checkgroupIds.length > 0){
            //绑定套餐和检查组的多对多关系
            setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        }
        //将图片名称保存到Redis
        savePic2Redis(setmeal.getImg());

    }

    private void setSetmealAndCheckGroup(Integer id, Integer[] checkgroupIds) {
        for (Integer checkgroupId:checkgroupIds) {
            Map<String,Integer> map= new HashMap<>();
            map.put("setmeal_id",id);
            map.put("checkgroup_id",checkgroupId);
            setmealDao.setSetmealAndCheckGroup(map);

        }
    }

    //将图片名称保存到Redis

    private void savePic2Redis(String pic){

        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,pic);

    }

    //分页查询
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        return setmealDao.findCheckGroupIdsBySetmealId(id);
    }

    @Override
    //编辑检查组，同时需要更新和检查项的关联关系
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        // 使用套餐id，查询数据库对应的套餐，获取数据库存放的img
        Setmeal setmeal_db=setmealDao.findById(setmeal.getId());
        String img = setmeal_db.getImg();
        if (setmeal.getImg()!=null && !setmeal.getImg().equals(img)){
            QinUtils.deleteFileFromQiniu(img);
        }


        //根据检查组id删除中间表数据（清理原有关联关系）
        setmealDao.deleteAssociation(setmeal.getId());
        //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
        setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        //更新检查组基本信息
        setmealDao.edit(setmeal);

    }

    @Override
    public void deleteById(Integer id) {
        Setmeal setmeal = findById(id);
        //查询当前检查项是否和检查组关联
        int count = setmealDao.findSetmealAndCheckGroupCountBySetMealId(id);
        if (count>0){
            throw new RuntimeException("当前检查项被引用，不能删除");
        }
        setmealDao.deleteById(id);
        QinUtils.deleteFileFromQiniu(setmeal.getImg());
    }


}
